package cn.tedu.micro.demo.all.service;

import cn.tedu.micro.demo.all.feign.CartFeignClient;
import cn.tedu.micro.demo.all.feign.StockFeignClient;
import cn.tedu.micro.demo.all.mapper.OrderMapper;
import cn.tedu.micro.demo.common.pojo.po.OrderPO;
import cn.tedu.micro.demo.common.pojo.po.StockPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private CartFeignClient cartClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StockFeignClient stockClient;

    @Override
    public Boolean submitOrder(Integer userId, String productCode, Integer count) {
        ///stock/reduce?productCode=PC100&count=10 1000-990 990-980
        stockClient.updateStock(productCode,count);
        //2.生成订单 封装OrderPO 写入数据库
        OrderPO order=new OrderPO();
        order.setUserId(userId+"");
        order.setProductCode(productCode);
        order.setCount(count);
        //订单编号 保证唯一 使用uuid
        order.setOrderNo(UUID.randomUUID().toString());
        orderMapper.insert(order);
        //3.根据userId 和 productCode 删除购物车
        System.out.println("购物车客户端:"+cartClient.getClass().getName());
        cartClient.deleteCart(productCode,userId+"");
        return true;
    }
}
